package com.framework.bdf4j.msg.util;

import org.dozer.DozerBeanMapper;
import org.dozer.Mapper;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @Description TODO
 * @Author Minstrel
 * @Date 2020/8/3 2:09
 * @Version 1.0
 */
public class DtoEntityUtil {

    private static Mapper mapper = new DozerBeanMapper();

    /**
     *  对象转换
     * @param t
     * @param clazz
     * @param <D>
     * @param <E>
     * @return
     */
    public static <D,E> E trans(D t,Class<E> clazz){
        if(t == null)
            return null;
        return mapper.map(t, clazz);
    }

    /**
     * 集合转换
     * @param ts
     * @param clazz
     * @param <D>
     * @param <E>
     * @return
     */
    public static <D,E> List<E> trans(List<D> ts,Class<E> clazz){
        List<E> es = new ArrayList<>();
        if(ts == null )
            return es;
        for(D d:ts) {
            E e = (E)trans(d,clazz);
            if(e != null)
                es.add(e);
        }
        return es;
    }

}
